package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Quantitytype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("QuantitytypeController")
public class QuantitytypeController extends CmiEmaController {

	@RequestMapping("/listQuantitytypes")
	public ModelAndView listQuantitytypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = quantitytypePaginationService.updatePagination(request, pagination);
		Set<Quantitytype> quantitytypes = quantitytypePaginationService.getPaginationQuantitytypes(pagination);

		mav.addObject("quantitytypes", quantitytypes);
		mav.addObject("entityName", "quantitytype");
		mav.addObject("pagination", pagination);
		mav.setViewName("quantitytype/listQuantitytypes");
		return mav;
	}

	public String listQuantitytypes() {
		return "redirect:/listQuantitytypes";
	}

	@RequestMapping("/viewQuantitytype")
	public ModelAndView viewQuantitytype(@RequestParam Integer quantitytypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("quantitytype", quantitytypeDAO.findQuantitytypeByPrimaryKey(quantitytypeId));
		mav.setViewName("quantitytype/viewQuantitytype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteQuantitytypes")
	public ModelAndView confirmDeleteQuantitytypes(
			@RequestParam(value = "deleteQuantitytypesIds", required = true) Set<Integer> deleteQuantitytypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Quantitytype> deleteQuantitytypes = quantitytypeDAO.findQuantitytypesByIds(deleteQuantitytypesIds);
		Set<Quantitytype> referencedQuantitytypes = quantitytypeService.loadReferenceQuantitytypes(deleteQuantitytypes);
		boolean hasReferenced;
		if (referencedQuantitytypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteQuantitytypes);
			hasReferenced = false;
		} else {
			deleteQuantitytypes.removeAll(referencedQuantitytypes);
			mav.addObject("referencedEntitys", referencedQuantitytypes);
			mav.addObject("deleteEntitys", deleteQuantitytypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Quantitytype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listQuantitytypes");
		return mav;
	}

	@RequestMapping("/deleteQuantitytype")
	public String deleteQuantitytype(@RequestParam Integer quantitytypeId) {
		Quantitytype quantitytype = quantitytypeDAO.findQuantitytypeByPrimaryKey(quantitytypeId);
		quantitytypeService.deleteQuantitytype(quantitytype);
		return "forward:/listQuantitytypes";
	}

	@RequestMapping("/confirmDeleteQuantitytype")
	public ModelAndView confirmDeleteQuantitytype(@RequestParam Integer quantitytypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("quantitytype", quantitytypeDAO.findQuantitytypeByPrimaryKey(quantitytypeId));

		mav.setViewName("quantitytype/deleteQuantitytype");

		return mav;
	}

	@RequestMapping("/deleteQuantitytypes")
	public String deleteQuantitytypes(
			@RequestParam(value = "deleteQuantitytypesId", required = true) Set<Integer> deleteQuantitytypesId) {
		quantitytypeService.deleteQuantitytypes(deleteQuantitytypesId);
		return "forward:/listQuantitytypes";
	}

	@RequestMapping("/editQuantitytype")
	public ModelAndView editQuantitytype(@RequestParam Integer quantitytypeId) {

		ModelAndView mav = new ModelAndView();

		Quantitytype quantitytype = quantitytypeDAO.findQuantitytypeByPrimaryKey(quantitytypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("quantitytype", quantitytype);

		// 把所有的引用quantitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(quantitytypeService.loadQuantitytypeObjectReferencedSet(quantitytype));

		// 将编辑页面quantitytype添加到mav中
		mav.setViewName("quantitytype/editQuantitytype");
		return mav;
	}

	@RequestMapping("/newQuantitytype")
	public ModelAndView newQuantitytype() {
		ModelAndView mav = new ModelAndView();
		Quantitytype quantitytype = new Quantitytype();

		mav.addObject("quantitytype", quantitytype);

		// 把所有的引用quantitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(quantitytypeService.loadQuantitytypeObjectReferencedSet(quantitytype));

		mav.addObject("newFlag", true);
		mav.setViewName("quantitytype/editQuantitytype");

		return mav;
	}

	@RequestMapping("/saveQuantitytype")
	public ModelAndView saveQuantitytype(@ModelAttribute Quantitytype quantitytype) {

		ModelAndView mav = new ModelAndView();

		Quantitytype existingQuantitytype = CmiHibernateProxyUtil
				.deproxy(quantitytypeDAO.findQuantitytypeByPhysicalPrimaryKey(quantitytype.getName()));

		// 新建记录
		if (quantitytype.getId() == null) {
			if (quantitytype.equals(existingQuantitytype)) {

				mav.addAllObjects(quantitytypeService.loadQuantitytypeClassReferencedSet(quantitytype));
				// mav = editContinue(mav, quantitytype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("quantitytype/editQuantitytype");
			} else {
				quantitytypeService.saveQuantitytype(quantitytype);
				mav.setViewName("forward:/listQuantitytypes");
			}
		}
		// 编辑记录
		else {
			Quantitytype originQuantitytype = CmiHibernateProxyUtil
					.deproxy(quantitytypeDAO.findQuantitytypeByPrimaryKey(quantitytype.getId()));
			if (!quantitytype.equals(originQuantitytype) && quantitytype.equals(existingQuantitytype)) {
				mav.addAllObjects(quantitytypeService.loadQuantitytypeClassReferencedSet(quantitytype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("quantitytype/editQuantitytype");
			} else {
				quantitytypeService.saveQuantitytype(quantitytype);
				mav.setViewName("forward:/listQuantitytypes");
			}
		}
		return mav;
	}

	@RequestMapping("/quantitytypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
